package main

import (
	"fmt"
	"github.com/micro/go-micro"
	"github.com/micro/go-micro/broker"
	"github.com/micro/go-micro/config"
	"github.com/micro/go-micro/service/grpc"
	"github.com/micro/go-plugins/broker/rabbitmq"
	"log"
	"shopping/order/db"
	"shopping/order/handler"
	order "shopping/order/proto/order"
	"shopping/order/repository"
	product "shopping/product/proto/product"
	_ "github.com/micro/go-plugins/broker/rabbitmq"
)

func main() {
	repo := &repository.Order{db.MastDB}

	fmt.Println(config.Get("rabbitmq_addr").String(""))
	//broker
	b := rabbitmq.NewBroker(
		broker.Addrs(config.Get("rabbitmq_addr").String("")),
	)

	// boot trace
	//TraceBoot()

	// New Service
	service := grpc.NewService(
		micro.Name("go.micro.srv.order"),
		micro.Version("latest"),
		micro.Broker(b),
		//micro.WrapHandler(wrapperTrace.NewHandlerWrapper()),
		//micro.WrapClient(wrapperTrace.NewClientWrapper()),
		//micro.WrapHandler(wrapperPrometheus.NewHandlerWrapper()),
	)

	// boot prometheus
	//PrometheusBoot()

	// Initialise service
	service.Init()

	//创建消息发布者
	publisher := micro.NewPublisher("notification.submit", service.Client())

	//product-srv client
	productCli := product.NewProductService("go.micro.srv.product", service.Client())

	// Register Handler
	order.RegisterOrderServiceHandler(service.Server(), &handler.Order{repo, productCli, publisher})

	// Register Struct as Subscriber
	//micro.RegisterSubscriber("go.micro.srv.order", service.Server(), new(subscriber.Example))

	// Register Function as Subscriber
	//micro.RegisterSubscriber("go.micro.srv.order", service.Server(), subscriber.Handler)

	// Run service
	if err := service.Run(); err != nil {
		log.Fatal(err)
	}
}
